<template>
  <el-container v-loading="loading">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="180px" label-position="left" size="small">
      <el-form-item label="商户号" prop="mchid">
        <el-input v-model="ruleForm.mchid"></el-input>
      </el-form-item>

      <el-form-item label="商户密钥" prop="key">
        <el-input v-model="ruleForm.key"></el-input>
      </el-form-item>

      <el-form-item label="secret" prop="secret">
        <el-input v-model="ruleForm.secret"></el-input>
      </el-form-item>

      <el-form-item label="appid" prop="appid">
        <el-input v-model="ruleForm.appid"></el-input>
      </el-form-item>

      <el-form-item label="微信支付(apiclient_cert.pem)" prop="apiclient_cert">
        <el-input type="textarea" v-model="ruleForm.apiclient_cert"
                  placeholder="使用文本编辑器打开apiclient_cert.pem文件，将文件的全部内容复制进来"></el-input>
        <div class="form-item-tip">
          <span>提示：使用文本编辑器打开apiclient_cert.pem文件，将文件的全部内容复制进来</span>
        </div>
      </el-form-item>

      <el-form-item label="微信支付 (apiclient_key.pem)" prop="apiclient_key">
        <el-input type="textarea" v-model="ruleForm.apiclient_key"
                  placeholder="使用文本编辑器打开apiclient_cert.pem文件，将文件的全部内容复制进来"></el-input>
        <div class="form-item-tip">
          <span>提示：使用文本编辑器打开apiclient_cert.pem文件，将文件的全部内容复制进来</span>
        </div>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">立即保存</el-button>
        <!--<el-button @click="resetForm('ruleForm')">重置</el-button>-->
      </el-form-item>
    </el-form>

  </el-container>
</template>

<script>
  export default {
    data() {
      return {
        loading: true,  // 页面加载
        ruleForm: {
          appid: "",                //类型：String  必有字段  备注：小程序appid
          secret: "",                //类型：String  必有字段  备注：小程序密钥
          mchid: "",                //类型：String  必有字段  备注：商户号
          key: "",                //类型：String  必有字段  备注：商户密钥
          apiclient_cert: "",                //类型：String  必有字段  备注：apiclient_cert证书
          apiclient_key: ""                //类型：String  必有字段  备注：apiclient_key证书
        },
        rules: {
          appid: [
            {required: true, message: '小程序appid不得为空', trigger: 'blur'},
          ],
          secret: [
            {required: true, message: '小程序密钥不得为空', trigger: 'blur'},
          ],
          mchid: [
            {required: true, message: '商户号不得为空', trigger: 'blur'},
          ],
          key: [
            {required: true, message: '商户密钥不得为空', trigger: 'blur'},
          ],
          apiclient_cert: [
            {required: true, message: 'apiclient_cert证书不得为空', trigger: 'blur'},
          ],
          apiclient_key: [
            {required: true, message: 'apiclient_key不得为空', trigger: 'blur'},
          ],
        },
      };
    },
    methods: {
      submitForm(formName) {
        let _this = this;
        this.$refs[formName].validate((valid) => {
          if (valid) {
            _this.api.updatePaymentConfig(_this.ruleForm).then(function (res) {
              if (res.code == 1) {
                _this.$message({
                  showClose: true,
                  message: res.msg,
                  type: 'success'
                });
              } else {
                _this.$message({
                  showClose: true,
                  message: res.msg,
                  type: 'warning'
                });
              }
            }).catch(function (error) {
              throw new Error(error);
            });
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    },

    mounted: function () {
      let _this = this;
      this.api.getPaymentConfig().then(function (res) {
        if (res.code == 1) {
          _this.ruleForm = res.data;
        }
        _this.loading = false;
      }).catch(function (res) {
        throw new Error(error);
      });
    },
  }
</script>

<style scoped>

  .el-container {
    display: block;
  }

  /* 表单 */
  .el-form {
    width: 1000px;
    margin: 15px 0 15px 30px;
  }

  /* 图片上传 */
  .avatar-uploader {
    border: 1px dashed #d9d9d9;
    width: 178px;
  }

  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }

</style>
